define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/service/index' + location.search,
                    add_url: 'xiservice/service/add',
                    edit_url: 'xiservice/service/edit',
                    del_url: 'xiservice/service/del',
                    multi_url: 'xiservice/service/multi',
                    import_url: 'xiservice/service/import',
                    table: 'xiservice_service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                search: false,     
                showExport: false,
                showToggle: false,
                showColumns: false,                
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'city_name', title: '服务城市',operate:false},
                        {field: 'servicecategory.name', title: __('服务分类'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'image', title: __('Image'),width: '120',operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'is_chose', title: __('Is_chose'), searchList: {"0":__('Chose 0'),"1":__('Chose 1')}, formatter: Table.api.formatter.normal},
                        {field: 'bargain', title: __('Bargain'), searchList: {"0":__('Bargain 0'),"1":__('Bargain 1')}, formatter: Table.api.formatter.normal},
                        {field: 'saleprice', title: __('Saleprice'), operate: 'LIKE', formatter: function (val,row){
                            if(row.bargain==1){
                                return row.saleprice+"元起";
                            }else{
                                return row.saleprice+"元";
                            }
                        }},
                        {field: 'serviceprice', title: __('Serviceprice'), operate: 'LIKE',formatter: function (val,row){
                             return row.serviceprice+"元";
                        }},
                        {field: 'salenums', title: __('Salenums'),operate:false},
                        {field: 'quality_day', title: __('Quality day'),operate:false},
                        {field: 'lowprice', title: __('每平方价格/元'),operate:false},
                        // {field: 'quality_rate', title: '质保金(%)',operate:false},
                        {field: 'commission', title: '平台提成(%)',operate:false},
                     
                        // field: 'insure', title: '保险金(%)',operate:false},
                        // field: 'sale_rate', title: '业务提成(%)',operate:false},
                        // field: 'sale_rate', title: '业务提成(%)',operate:false},
                        // field: 'sale_rate', title: '业务提成(%)',operate:false},
                        // field: 'sale_rate', title: '业务提成(%)',operate:false},
                        // field: 'sale_rate', title: '业务提成(%)',operate:false},

                        // {field: 'insure', title: __('保险金(%)'),operate:false},
                        // {field: 'quality_year_one', title: __('质保年限'),operate:false},
                        // {field: 'quality_year_rate_one', title: __('质保一年费用'),operate:false},
                        // {field: 'quality_year_two', title: __('质保年限'),operate:false},
                        // {field: 'quality_year_rate_two', title: __('质保二年费用'),operate:false},
                        // {field: 'quality_year_there', title: __('质保年限'),operate:false},
                        // {field: 'quality_year_rate_there', title: __('质保三年费用'),operate:false},

                        // {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiservice/service/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiservice/service/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiservice/service/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
             $(document).on('change','#c-service_time_type',function () {
                var value = $(this).val();
                console.log(value);
                if(value == 1){
                    //$('#c-class_hour').parents('.form-group').addClass('hide');
                    $('#c-service_duration').parents('.form-group').removeClass('hide');
                    $('#c-service_time_unit').parents('.form-group').addClass('hide');
        
                }else if(value == 2){
                    $('#c-service_duration').parents('.form-group').addClass('hide');
                    $('#c-service_time_unit').parents('.form-group').removeClass('hide');               
                }else{
                    $('#c-service_duration').parents('.form-group').addClass('hide');
                    $('#c-service_time_unit').parents('.form-group').addClass('hide');                      
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
             $(document).on('change','#c-service_time_type',function () {
                var value = $(this).val();
                console.log(value);
                if(value == 1){
                    //$('#c-class_hour').parents('.form-group').addClass('hide');
                    $('#c-service_duration').parents('.form-group').removeClass('hide');
                    $('#c-service_time_unit').parents('.form-group').addClass('hide');
        
                }else if(value == 2){
                    $('#c-service_duration').parents('.form-group').addClass('hide');
                    $('#c-service_time_unit').parents('.form-group').removeClass('hide');               
                }else{
                    $('#c-service_duration').parents('.form-group').addClass('hide');
                    $('#c-service_time_unit').parents('.form-group').addClass('hide');                      
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
